﻿
$(function(){
    var request = moon.getRequest(location.search); //获取url中"?"符后的参数
    var deviceId = request['deviceId'];  //获取设备deviceId

    //填充数据
    $.getJsonData(contextPath+"/device/"+deviceId,{},{type:"Get"}).done(function (data) {
        console.log(data);
        if(data.success){
            var result = data.result;
            $("#title").val(result.title);
            $("#tags").val(result.tags);
            $("#about").val(result.about);
            $("#local").val(result.local);
            $("#site_flag").val(result.site_flag ? result.site_flag:0);
        }
    })

    //更新
    $("#submit").click(function(){
        $("#deviceForm").validate("validate").done(function(result){
            var device={
                title:$("#title").val(),
                tags:$("#tags").val(),
                about:$("#about").val(),
                local:$("#local").val(),
                site_flag:$("#site_flag").val()
            }
            console.log(device);
            $.getJsonData(contextPath+"/device/"+deviceId,device,{type:"Put"}).done(function(data){
                console.log(data);
                if(data.success){
                    window.location.href=contextPath+"dev_suc_edit.html?deviceId="+deviceId;
                }else{
                    moon.error("修改成功","top");
                }
            }).fail(function(e){
                console.log(e);
            });
        })
    })

    $("#loginOut").on("click",function(){
        $.getJsonData(fullServerPath+"/users/logout",{},{type:"Post"}).done(function(data){
            if(data.success){
                moon.info("退出","top");
                window.location.href = contextPath+"login.html";
            }else{
                moon.error("退出失败","top");
            }
        })
    })

    //添加表单验证
    $("#deviceForm").validate({align:'bottom',theme:"darkblue"});

})
